package com.example.controller;


import com.example.entity.User;
import com.example.entity.User0;
import com.example.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {

    @Autowired
    UserRepository userRepository;

    @Autowired
    User0 user0;

    @RequestMapping("/user/login")
    public String login(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            Model model, HttpSession session){

//具体的业务
        if(userRepository.existsByName(username)){//判断用户是否存在
            User user = userRepository.findUserByName(username);
            if (user.getPwd().equals(password)){
                session.setAttribute("loginUser",user);
                user0.setUser(user);
                return "redirect:/main";
            }else {
                //告诉用户登录失败
                model.addAttribute("msg","用户名或密码错误");
                return  "index";
            }

        }else {
            //告诉用户登录失败
            model.addAttribute("msg","用户名不存在");
            return  "index";
        }
    }

    @RequestMapping("/user/register")
    public String register(
            @RequestParam("username") String username,
            @RequestParam("password") String password,
            @RequestParam("password2") String password2,
            Model model, HttpSession session) {

        //去除空格
        String username1 = username.replaceAll(" ", "");
        String password1 = password.replaceAll(" ", "");
        String password22 = password2.replaceAll(" ", "");
//具体的业务
        if (username.contains(" ")) {//判断用户名是否有空格
            model.addAttribute("msg", "用户名不能有空格，请重新输入");
            model.addAttribute("username",username1);
            return "/register";
        }
        if (password.contains(" ")) {//判断用户名是否有空格
            model.addAttribute("msg", "密码不能有空格，请重新输入");
            return "/register";
        }
        if (userRepository.existsByName(username1)) {//判断用户是否存在
            model.addAttribute("msg", "用户名已存在");
            return "/register";
        }
        if (password1.equals(password22)){
            List<User> users = userRepository.findAll();
            User user = users.get(users.size()-1);//获得最后一个用户
            User user1 = new User(user.getId()+1, username, password);//新建用户
            userRepository.saveAndFlush(user1);
            return "redirect:/";
        } else {
            model.addAttribute("msg", "两次密码输入不一致");
            return "/register";
        }
    }

    @RequestMapping("/user/logout")
    public String logout(HttpSession session,Model model){
        session.invalidate();
        return "redirect:/";
    }
}
